package com.example.yucheng.controller;
import com.example.yucheng.domain.Stock;
import com.example.yucheng.domain.Storage;
import com.example.yucheng.service.StockService;
import com.example.yucheng.service.StorageService;
import com.example.yucheng.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    StorageService storageService;
    @Autowired
    StockService stockService;


    @GetMapping("/getStorageList")
    public Result getStorageList(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam(defaultValue = "1") int sort){
        Map<String, Object> map = new HashMap<>();
        pageNum-=1;
        pageNum*=pageSize;
        List<Storage> storageList;
        if (sort == 1) {
            storageList = storageService.getStorageList(pageNum, pageSize);
        }else{
            storageList=storageService.getStorageSortList(pageNum, pageSize);
        }
        List<Storage> allStorage = storageService.getAllStorage();
        map.put("storageList",storageList);
        map.put("total",allStorage.size());
        return Result.success(map);
    }

    @PutMapping("/updateStorage")
    public Result updateStorage(@RequestBody Storage storage){
        Storage storage2 = storageService.findStorageById(storage.getId());
        Stock stock = storageService.findGood(storage2.getGId());
        if (stock.getNumber() - storage2.getNumber() + storage.getNumber() < 0)
            return Result.success("修改失败，商品数量不足");
        stock.setNumber(stock.getNumber() - storage2.getNumber() + storage.getNumber());
        stockService.updateStockNumber(stock);
        storageService.updateStorage(storage);
        return Result.success("修改成功");
    }

    @DeleteMapping("/deleteStorage")
    public Result deleteStorage(@RequestParam Integer [] ids){
        for(int id : ids){
            Storage storage = storageService.findStorageById(id);
            Stock stock = storageService.findGood(storage.getGId());
            stock.setNumber(stock.getNumber() - storage.getNumber());
            storageService.deleteStorage(id);
        }
        return Result.success("删除成功");
    }

    @PostMapping("/addStorage")
    public Result addStorage(@RequestBody Storage storage){
        System.out.println(storage.getGId());
        //查看仓库是否有
        Stock stock = storageService.findGood(storage.getGId());
        System.out.println(stock);
        if (stock != null) {
            if (stock.getNumber() == null)
                stock.setNumber((long)0);
            //有将仓库的数量进行更新
            stock.setNumber(stock.getNumber()+ storage.getNumber());
            stockService.updateStockNumber(stock);
            //新增入库记录
            storage.setCId(stock.getCId());
            storageService.addStorage(storage);
            return Result.success("增加成功");
        }else{
            return Result.success("添加失败,仓库未有该商品");
        }
    }

}
